import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:proj_sfv/component/Chat.dart';
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';
import 'package:tencent_cloud_chat_uikit/ui/controller/tim_uikit_chat_controller.dart';
import 'package:tencent_cloud_chat_uikit/ui/controller/tim_uikit_conversation_controller.dart';
import 'package:tencent_cloud_chat_uikit/ui/views/TIMUIKitConversation/tim_uikit_conversation_item.dart';


class MessagePage extends StatefulWidget {
  @override
  State<MessagePage> createState() => _MessagePageState();

  const MessagePage();
}

class _MessagePageState extends State<MessagePage> {
  final TIMUIKitConversationController _controller = TIMUIKitConversationController();

  @override
  Widget build(BuildContext context) {
    void _handleOnConvItemTaped(V2TimConversation selectedConv) async {
      await Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => Chat(
              selectedConversation: selectedConv,
            ),
          ));
      _controller.reloadData();
    }


    Widget _itemBuilder(V2TimConversation conversationItem,
        [V2TimUserStatus onlineStatus]) {
      return InkWell(
        child: TIMUIKitConversationItem(
          isShowDraft: true,
            faceUrl: conversationItem.faceUrl ?? "",
            nickName: conversationItem.showName ?? "",
            isDisturb: conversationItem.recvOpt != 0,
            lastMsg: conversationItem.lastMessage,
            isPined: conversationItem.isPinned ?? false,
            groupAtInfoList: conversationItem.groupAtInfoList ?? [],
            unreadCount: conversationItem.unreadCount ?? 0,
            draftText: conversationItem.draftText,
            onlineStatus: (true &&
                conversationItem.userID != null &&
                conversationItem.userID.isNotEmpty)
                ? onlineStatus
                : null,
            draftTimestamp: conversationItem.draftTimestamp,
            convType: conversationItem.type
        ),
        onTap: () => _handleOnConvItemTaped(conversationItem),
      );
    }



    return Scaffold(
      body: TIMUIKitConversation(
        onTapItem: (selectedConv) {
          Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => Chat(
                  selectedConversation: selectedConv,
                ),
              ));
        },
        emptyBuilder: () {
          return Container(
            padding: const EdgeInsets.only(top: 100),
            child: Center(
              child: Text("暂无会话"),
            ),
          );
        },
        itembuilder: _itemBuilder,
      ),
    );
  }
}